from openphantasy.utils import json_response
from openphantasy.server.models import Country

@json_response
def add(request):
    """expecting POST data:  
    - code: the code of the country
    - name: the name of the country
    """
    code = request.REQUEST["code"]
    name = request.REQUEST["name"]
    country = Country(code=code, name=name)
    country.save()
    return {"code":code, "name":name}

@json_response
def fetch(request):
    """if no parameter is passed in, return all objects.
    otherwise, return according to the given criteria.
    """
    return [ x.__on__() for x in Country.objects.all() ]

